<template>
  <div class="home">
    <div class="header">
      <div class="search">
        <el-input
          placeholder="请输入用户信息"
          v-model="userInfo"
          suffix-icon="el-icon-search"
        >
        </el-input>
        <el-button type="primary" size="small" @click="searchUser"
          >搜索</el-button
        >
      </div>
      <div class="add">
        <el-button type="primary" size="small" @click="addVisible = true"
          >添加用户</el-button
        >
      </div>
    </div>
    <el-table :data="userList" border style="width: 70%">
      <el-table-column prop="name" label="姓名" width="70"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="50"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="70"> </el-table-column>
      <el-table-column prop="phone" label="手机号" width="150">
      </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column prop="address" label="操作">
        <template v-slot="{ row }">
          <el-button type="warning" size="small" @click="edit(row)"
            >修改用户</el-button
          >
          <el-button type="danger" size="small" @click="delUser(row)"
            >删除用户</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="addVisible" width="50%" close>
      <el-form ref="form" :model="form[0]" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="请选择性别" size="100%">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户对话框 -->
    <el-dialog title="修改用户" :visible.sync="editVisible" width="50%">
      <el-form :model="editForm[0]" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="editForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select
            v-model="editForm.sex"
            placeholder="请选择性别"
            size="100%"
          >
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="editForm.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="editForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input
            v-model="editForm.address"
            placeholder="请输入地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      userInfo: "",
      userList: [
        {
          name: "王小虎",
          sex: "男",
          age: 18,
          phone: "13811112221",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          name: "张三",
          sex: "男",
          age: 20,
          phone: "13811112222",
          address: "上海市普陀区金沙江路 1517 弄"
        },
        {
          name: "小美",
          sex: "女",
          age: 17,
          phone: "13811112223",
          address: "上海市普陀区金沙江路 1519 弄"
        },
        {
          name: "李四",
          sex: "男",
          age: 26,
          phone: "13811112224",
          address: "上海市普陀区金沙江路 1516 弄"
        }
      ],
      addVisible: false,
      editVisible: false,
      form: [
        {
          name: "",
          sex: "",
          age: "",
          address: "",
          phone: ""
        }
      ],
      editForm: [
        {
          name: "",
          sex: "",
          age: "",
          address: "",
          phone: ""
        }
      ]
    };
  },
  methods: {
    searchUser() {
      this.userList = this.userList.filter(item => {
        return item.name == this.userInfo;
      });
      this.userInfo = '';
    },
    addUser() {
      this.userList.push(this.form);
      this.form = "";
      this.$message({
        message: "添加用户成功",
        type: "success"
      });
      this.addVisible = false;
    },
    edit(row) {
      this.editVisible = true;
      this.editForm = row;
    },
    editUser() {
      this.$message({
        message: "修改用户成功",
        type: "success"
      });
      this.editVisible = false;
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.userList = this.userList.filter(item => {
            return item.phone !== row.phone;
          });
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style lang="less" scoped>
body {
  background-color: #eee;
  .el-table {
    margin: auto;
  }
}
.header {
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 100px 10px 10px 10px;
  .search {
    display: flex;
    .el-input {
      margin-right: 20px;
    }
  }
}
.el-select {
  display: block;
}
</style>

